package edu.jsnu.purchase.ticket.service.impl;

import edu.jsnu.purchase.ticket.service.ScheduleService;
import edu.jsnu.service.ticket.schedule.dao.ScheduleMapper;
import edu.jsnu.service.ticket.schedule.dto.ScheduleBrief;
import edu.jsnu.service.ticket.schedule.dto.ScheduleShow;
import edu.jsnu.service.ticket.seat.dao.SeatMapper;
import edu.jsnu.service.ticket.seat.domain.Seat;
import edu.jsnu.service.ticket.seat.dto.SeatVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private SeatMapper seatMapper;


    @Override
    public List<ScheduleBrief> getBriefSchedule(Integer filmId, String date) {
        String[] dates = null;
        if (StringUtils.isNotEmpty(date)){
            dates = date.split("-");
        }
        List<ScheduleBrief> scheduleList = scheduleMapper.getBriefSchedule(filmId, dates[0], dates[1]);
        return scheduleList;
    }

    @Override
    public ScheduleShow getScheduleInfo(Integer scheduleId) {
        return scheduleMapper.getScheduleInfo(scheduleId);
    }

    @Override
    public List<List<SeatVo>> getSeatInfo(Integer scheduleId) {
        List<List<SeatVo>> seatList = new ArrayList<>();
        int count = 1;
        //座位56个7排*8座
        for (int i = 0; i < 7; i++){
            List<SeatVo> seatInfo = new ArrayList<>();
            for (int j = 0; j< 8; j++){
                Seat seat = seatMapper.selectBySeatAndSchedule(count, scheduleId);
                if (seat == null){
                    seatInfo.add(new SeatVo(count, 0));
                }else {
                    seatInfo.add(new SeatVo(count, 1));
                }
                count++;
            }
            seatList.add(seatInfo);
        }
        return seatList;
    }


}
